package org.xhy.sniffer.plugins.mysql;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.xhy.sniffer.plugin.ClassEnhancePluginDefine;
import org.xhy.sniffer.interceptor.ConstructorMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.InstanceMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.StaticMethodsInterceptorPoint;
import org.xhy.sniffer.match.ClassMatch;
import org.xhy.sniffer.match.ClassNameMatch;

import static net.bytebuddy.matcher.ElementMatchers.named;


public class MysqlPreparedStatementInstrumentation extends ClassEnhancePluginDefine {
    private static final String CLIENT_PS_NAME = "com.mysql.cj.jdbc.ClientPreparedStatement";
    private static final String SERVER_PS_NAME = "com.mysql.cj.jdbc.ServerPreparedStatement";
    private static final String INTERCEPTOR = "org.xhy.sniffer.plugins.mysql.interceptor.MySQLPreparedStatementInterceptor";
    @Override
    protected ClassMatch enhanceClass() {
        return ClassNameMatch.buildClassNameMatch(CLIENT_PS_NAME,SERVER_PS_NAME);
    }

    @Override
    protected InstanceMethodsInterceptorPoint[] getInstanceMethodsInterceptorPoints() {
        return new InstanceMethodsInterceptorPoint[]{
                new InstanceMethodsInterceptorPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return named("execute")
                                .or(named("executeUpdate"))
                                .or(named("executeQuery"));
                    }

                    @Override
                    public String getMethodsInterceptor() {
                        return INTERCEPTOR;
                    }
                },
        };
    }

    @Override
    protected ConstructorMethodsInterceptorPoint[] getConstructorMethodsInterceptorPoints() {
        return new ConstructorMethodsInterceptorPoint[0];
    }

    @Override
    protected StaticMethodsInterceptorPoint[] getStaticMethodsInterceptorPoints() {
        return new StaticMethodsInterceptorPoint[0];
    }
}
